home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / c++advio 2.3 / Advanced i⁄o / vmyenv.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-05  |  4.7 KB  |  163 lines  |  [TEXT/ALFA]

  1. // This may look like C code, but it is really -*- C++ -*-
  2. //            Validate myenv
  3. //
  4. // $Id: vmyenv.cc,v 2.0 1997/03/04 22:15:08 oleg Exp oleg $
  5.  
  6. #include "myenv.h"
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <fstream.h>
  10.  
  11.  
  12.         // Make sure that evaluation of 'STMT' aborts,
  13.         // (as it was supposed to)
  14. #ifndef unix
  15. #define must_have_failed(STMT) \
  16.   cout << "can't check failure of " #STMT " on non-UNIX platforms" << endl
  17. #else
  18. #include <sys/wait.h>
  19. #define must_have_failed(STMT)                 \
  20.  if( const pid_t child_pid = fork() )            \
  21.  {                            \
  22.    assert( child_pid != (pid_t)(-1) );            \
  23.    int stat_loc;                    \
  24.    assert( waitpid( child_pid,&stat_loc,0 ) >= 0 );    \
  25.    assure( stat_loc != 0, #STMT " didn't crash as expected" ); /* the child has really crashed */    \
  26.    cerr << "The error above was expected, and caught" << endl;    \
  27.  }                            \
  28.  else { /*in the child process*/ (void) STMT; exit(0); }
  29. #endif
  30.  
  31.                 // Validate does_start_with_ci()
  32. static void test_does_start_with(void)
  33. {
  34.   cout << "\nValidating does_start_with_ci..." << endl;
  35.  
  36.   assert( does_start_with_ci("abcdef ","abc") );
  37.   assert( does_start_with_ci(" abcdef "," abc") );
  38.   assert( does_start_with_ci("abc","abc") );
  39.   assert( does_start_with_ci("aBc","AbC") );
  40.   assert( does_start_with_ci("aBc\n","AbC") );
  41.   assert( !does_start_with_ci("aBe\n","AbC") );
  42.   assert( !does_start_with_ci("acc","abc") );
  43.   assert( !does_start_with_ci("","abc") );
  44.   assert( does_start_with_ci("abc","") );
  45.   assert( !does_start_with_ci("ab","abc") );
  46.   assert( does_start_with_ci("a123456789.,a","A123456789.,") );
  47.   assert( does_start_with_ci("a1234bB?\\def","A1234Bb?\\") );
  48.   assert( does_start_with_ci("\rZ!@~#$Ll*()def","\rz!@~#$lL*()") );
  49.   assert( !does_start_with_ci("a1234bB.\\def","A1234Bb?\\") );
  50.  
  51.   cout << "\nDone" << endl;
  52. }
  53.  
  54.  
  55. #ifdef macintosh
  56.                 // Validate xgetenv()
  57. static void test_xgetenv(void)
  58. {
  59.   cout << "\nCan't use getenv on a Mac at present..." << endl;
  60. }
  61. #else
  62.                 // Validate xgetenv()
  63. static void test_xgetenv(void)
  64. {
  65.   cout << "\nValidating xgetenv..." << endl;
  66.   
  67.               // something that probably doesn't exist by default
  68.   const char not_existing_env_name [] = "___XXXY"; 
  69.               // something that probably _does_ exist by default
  70.   const char pre_existing_env_name [] = "HOME"; 
  71.                           
  72.   assert(
  73.       strcmp(xgetenv(not_existing_env_name,"---NOT FOUND---"),
  74.              "---NOT FOUND---") == 0 );
  75.   assert( xgetenv(not_existing_env_name,"")[0] == '\0' );
  76.  
  77.   must_have_failed(xgetenv(not_existing_env_name,0));
  78.   
  79.   char buffer[100];
  80.   const char newly_bound_value [] = "___NEWLY_BOUND___";
  81.   assert( putenv(
  82.       (sprintf(buffer,"%s=%s",not_existing_env_name,newly_bound_value),
  83.       buffer)) == 0 );
  84.       
  85.   assert(
  86.       strcmp(xgetenv(not_existing_env_name,"---NOT FOUND---"),
  87.              newly_bound_value) == 0 );
  88.              
  89.   assert(
  90.       strcmp(xgetenv(not_existing_env_name,""),
  91.              newly_bound_value) == 0 );
  92.              
  93.   assert(
  94.       strcmp(xgetenv(not_existing_env_name,0),
  95.              newly_bound_value) == 0 );
  96.           
  97.   assert( xgetenv(pre_existing_env_name,0) );
  98.  
  99.   cout << "\nDone" << endl;
  100. }
  101. #endif
  102.  
  103.                 // Validate get_file_size()
  104. static void test_get_file_size(void)
  105. {
  106.   cout << "\nValidating get_file_size..." << endl;
  107.  
  108.   const char * file_name = tmpnam(0);
  109.   
  110.   remove(file_name);        // make sure this file does not exist
  111.   
  112.   cout << "\tchecking status of _non_-existing file " << file_name << endl;
  113.   
  114.   must_have_failed(get_file_size(file_name));
  115.  
  116.   struct GFS_big_default : public GFS_Default
  117.   {
  118.     enum { def_size = (size_t)(-2) };
  119.     size_t operator () (const char * file_name)
  120.      { cerr << "GFS_big_default entered for " <<  file_name << endl;
  121.        return def_size;
  122.      }
  123.   };
  124.   
  125.   assert( get_file_size(file_name,GFS_big_default()) ==
  126.           GFS_big_default::def_size);
  127.           
  128.   cout << "\tcreating " << file_name << " of zero size " << endl;
  129.   assert( ofstream(file_name).good() );
  130.   cout << "\t\tThe file size was found to be " << get_file_size(file_name) << endl;
  131.   assert( get_file_size(file_name,GFS_big_default()) == 0 );
  132.   assert( get_file_size(file_name) == 0 );
  133.  
  134.   const char sample_string [] = "12345";
  135.   
  136.   cout << "\twriting " << sample_string << " into " 
  137.        << file_name << endl;
  138.    
  139.   assert( (ofstream(file_name) << sample_string).good() );
  140.   cout << "\t\tThe file size was found to be " << get_file_size(file_name) << endl;
  141.   assert( get_file_size(file_name,GFS_big_default()) == strlen(sample_string) );
  142.   assert( get_file_size(file_name) == strlen(sample_string) );
  143.  
  144.   cout << "\nDone" << endl;
  145. }
  146.  
  147.  
  148. /*
  149.  *------------------------------------------------------------------------
  150.  *            Root module
  151.  */
  152.  
  153. main()
  154. {
  155.   cout << "\n\n\t\tVerify that myenv does function indeed\n" << endl;
  156.  
  157.   test_does_start_with();
  158.   test_xgetenv();
  159.   test_get_file_size();
  160.  
  161.   cout << "\nAll tests passed" << endl;
  162. }
  163.